package com.cskaoyan.service;

import com.cskaoyan.bean.ad.GoodsAndCatage;
import com.cskaoyan.bean.ad.IndexListVO;
import com.cskaoyan.bean.domain.*;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @author looper
 * @
 **/
@Service
public class HomeIndexServiceImpl implements HomeIndexService {

    @Autowired
    CouponService couponService;
    @Resource
    AdMapper adMapper;
    @Resource
    BrandMapper brandMapper;
    @Resource
    CategoryMapper categoryMapper;
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    TopicMapper topicMapper;


    @Override
    public IndexListVO getHomeIndex() {


        Properties properties = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File("src/main/resources/config.properties"));
            properties = new Properties();
            properties.load(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // TODO: 2021/12/4 查询数据库
        Integer newGoodsNum = Integer.parseInt(properties.getProperty("market_wx_index_new"));
        Integer catlogNum = Integer.parseInt(properties.getProperty("market_wx_catlog_list"));
        Integer hotGoodsNum = Integer.parseInt(properties.getProperty("market_wx_index_hot"));
        Integer catlogGoodsNum = Integer.parseInt(properties.getProperty("market_wx_catlog_goods"));
        Integer brandNum = Integer.parseInt(properties.getProperty("market_wx_index_brand"));
        Integer topicNum = Integer.parseInt(properties.getProperty("market_wx_index_topic"));

        //返回的容器
        IndexListVO indexListVO = new IndexListVO();
        List<Coupon> coupons = couponService.couponsWithOutPage();
        //couponList
        indexListVO.setCouponList(coupons);
        //banner
        AdExample adExample = new AdExample();
        adExample.createCriteria().andDeletedEqualTo(false).andEnabledEqualTo(true);
        List<Ad> banner = adMapper.selectByExample(adExample);
        indexListVO.setBanner(banner);
        //brandlist
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(0);
        List<Brand> brandList = brandMapper.selectByExample(brandExample);
        List<Brand> brandListLimit4 = brandList.stream().limit(brandNum).collect(Collectors.toList());
        indexListVO.setBrandList(brandListLimit4);
        //channel pid=0
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<Category> channel = categoryMapper.selectByExample(categoryExample);
        List<Category> categoryList = channel.stream().limit(catlogNum).collect(Collectors.toList());
        indexListVO.setChannel(categoryList);
        //floorGoodsList
        //先查询l2的目录 在根据目录查询商品 放进去
        String level = "L2";
        List<GoodsAndCatage> goodsAndCatage = categoryMapper.selectByLevel(level);
        for (GoodsAndCatage andCatage : goodsAndCatage) {
            Integer catageId = andCatage.getId();
            //查询所有catageId等于这个      goodsMapper.select
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
            criteria1.andCategoryIdEqualTo(catageId);
            criteria1.andDeletedEqualTo(false);
            List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
            List<Goods> goods = goodsList.stream().limit(catlogGoodsNum).collect(Collectors.toList());
            andCatage.setGoodsList(goods);
        }

        // TODO: 2021/12/4 商品二级类目数量
        List<GoodsAndCatage> collect = goodsAndCatage.stream().limit(catlogNum * 4).collect(Collectors.toList());


        indexListVO.setFloorGoodsList(collect);
        // TODO: 2021/12/3   grouponList团购先为null
//        indexListVO.setGrouponList(null);
        //查询ishot 1 的 isonsale 1 并且排序按照权重sortOrder
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andIsHotEqualTo(true)
                .andIsOnSaleEqualTo(true)
                .andDeletedEqualTo(false);
        goodsExample.setOrderByClause("sort_order");
        List<Goods> hotGoodsList = goodsMapper.selectByExample(goodsExample);
        List<Goods> goodsListHot = hotGoodsList.stream().limit(hotGoodsNum).collect(Collectors.toList());
        indexListVO.setHotGoodsList(goodsListHot);
        //查询newgoods
        GoodsExample newGoodsExample = new GoodsExample();
        GoodsExample.Criteria newGoodsExampleCriteria = newGoodsExample.createCriteria();
        newGoodsExampleCriteria.andIsNewEqualTo(true)
                .andIsOnSaleEqualTo(true)
                .andDeletedEqualTo(false);
        newGoodsExample.setOrderByClause("sort_order");
        List<Goods> newGoodsList = goodsMapper.selectByExample(newGoodsExample);
        List<Goods> goodListNew = newGoodsList.stream().limit(newGoodsNum).collect(Collectors.toList());
        indexListVO.setNewGoodsList(goodListNew);
        //topicList
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        List<Topic> topicList = topics.stream().sorted(Comparator.comparingInt(Topic::getSortOrder))
                .limit(topicNum)
                .collect(Collectors.toList());
        indexListVO.setTopicList(topicList);
        return indexListVO;
    }
}
